home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / dix / RCS / grabs.c,v < prev    next >
Encoding:
Text File  |  1990-02-14  |  10.9 KB  |  441 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.02.14.15.23.29;  author tve;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Original X11R4 distribution
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* $XConsortium: grabs.c,v 5.7 89/12/11 15:43:47 keith Exp $ */
  27. /************************************************************
  28. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  29. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  30.  
  31.                         All Rights Reserved
  32.  
  33. Permission to use, copy, modify, and distribute this software and its 
  34. documentation for any purpose and without fee is hereby granted, 
  35. provided that the above copyright notice appear in all copies and that
  36. both that copyright notice and this permission notice appear in 
  37. supporting documentation, and that the names of Digital or MIT not be
  38. used in advertising or publicity pertaining to distribution of the
  39. software without specific, written prior permission.  
  40. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  41. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  42. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  43. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  44. WHETHER IN AN action OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  45. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. SOFTWARE.
  47.  
  48. ********************************************************/
  49.  
  50. #include "X.h"
  51. #include "misc.h"
  52. #define NEED_EVENTS
  53. #include "Xproto.h"
  54. #include "windowstr.h"
  55. #include "inputstr.h"
  56. #include "cursorstr.h"
  57.  
  58. extern InputInfo inputInfo;
  59.  
  60. #define BITMASK(i) (((Mask)1) << ((i) & 31))
  61. #define MASKIDX(i) ((i) >> 5)
  62. #define MASKWORD(buf, i) buf[MASKIDX(i)]
  63. #define BITSET(buf, i) MASKWORD(buf, i) |= BITMASK(i)
  64. #define BITCLEAR(buf, i) MASKWORD(buf, i) &= ~BITMASK(i)
  65. #define GETBIT(buf, i) (MASKWORD(buf, i) & BITMASK(i))
  66.  
  67. GrabPtr
  68. CreateGrab(client, device, window, eventMask, ownerEvents, keyboardMode,
  69.        pointerMode, modDevice, modifiers, type, keybut, confineTo, cursor)
  70.     int client;
  71.     DeviceIntPtr device;
  72.     WindowPtr window;
  73.     Mask eventMask;
  74.     Bool ownerEvents, keyboardMode, pointerMode;
  75.     DeviceIntPtr modDevice;
  76.     unsigned short modifiers;
  77.     int type;
  78.     KeyCode keybut;    /* key or button */
  79.     WindowPtr confineTo;
  80.     CursorPtr cursor;
  81. {
  82.     GrabPtr grab;
  83.  
  84.     grab = (GrabPtr)xalloc(sizeof(GrabRec));
  85.     if (!grab)
  86.     return (GrabPtr)NULL;
  87.     grab->resource = FakeClientID(client);
  88.     grab->device = device;
  89.     grab->coreGrab = ((device == inputInfo.keyboard) ||
  90.               (device == inputInfo.pointer));
  91.     grab->window = window;
  92.     grab->eventMask = eventMask;
  93.     grab->ownerEvents = ownerEvents;
  94.     grab->keyboardMode = keyboardMode;
  95.     grab->pointerMode = pointerMode;
  96.     grab->modifiersDetail.exact = modifiers;
  97.     grab->modifiersDetail.pMask = NULL;
  98.     grab->modifierDevice = modDevice;
  99.     grab->coreMods = ((modDevice == inputInfo.keyboard) ||
  100.               (modDevice == inputInfo.pointer));
  101.     grab->type = type;
  102.     grab->detail.exact = keybut;
  103.     grab->detail.pMask = NULL;
  104.     grab->confineTo = confineTo;
  105.     grab->cursor = cursor;
  106.     if (cursor)
  107.     cursor->refcnt++;
  108.     return grab;
  109.  
  110. }
  111.  
  112. static void
  113. FreeGrab(pGrab)
  114.     GrabPtr pGrab;
  115. {
  116.     if (pGrab->modifiersDetail.pMask != NULL)
  117.     xfree(pGrab->modifiersDetail.pMask);
  118.  
  119.     if (pGrab->detail.pMask != NULL)
  120.     xfree(pGrab->detail.pMask);
  121.  
  122.     if (pGrab->cursor)
  123.     FreeCursor(pGrab->cursor, (Cursor)0);
  124.  
  125.     xfree(pGrab);
  126. }
  127.  
  128. /*ARGSUSED*/
  129. int
  130. DeletePassiveGrab(pGrab, id)
  131.     GrabPtr pGrab;
  132.     XID   id;
  133. {
  134.     register GrabPtr g, prev;
  135.  
  136.     /* it is OK if the grab isn't found */
  137.     prev = 0;
  138.     for (g = (wPassiveGrabs (pGrab->window)); g; g = g->next)
  139.     {
  140.     if (pGrab == g)
  141.     {
  142.         if (prev)
  143.         prev->next = g->next;
  144.         else
  145.         if (!(pGrab->window->optional->passiveGrabs = g->next))
  146.             CheckWindowOptionalNeed (pGrab->window);
  147.         break;
  148.     }
  149.     prev = g;
  150.     }
  151.     FreeGrab(pGrab);
  152. }
  153.  
  154. static Mask *
  155. DeleteDetailFromMask(pDetailMask, detail)
  156.     Mask *pDetailMask;
  157.     unsigned short detail;
  158. {
  159.     register Mask *mask;
  160.     register int i;
  161.  
  162.     mask = (Mask *)xalloc(sizeof(Mask) * MasksPerDetailMask);
  163.     if (mask)
  164.     {
  165.     if (pDetailMask)
  166.         for (i = 0; i < MasksPerDetailMask; i++)
  167.         mask[i]= pDetailMask[i];
  168.     else
  169.         for (i = 0; i < MasksPerDetailMask; i++)
  170.         mask[i]= ~0L;
  171.     BITCLEAR(mask, detail);
  172.     }
  173.     return mask; 
  174. }
  175.  
  176. static Bool
  177. IsInGrabMask(firstDetail, secondDetail, exception)
  178.     DetailRec firstDetail, secondDetail;
  179.     unsigned short exception;
  180. {
  181.     if (firstDetail.exact == exception)
  182.     {
  183.     if (firstDetail.pMask == NULL)
  184.         return TRUE;
  185.     
  186.     /* (at present) never called with two non-null pMasks */
  187.     if (secondDetail.exact == exception)
  188.         return FALSE;
  189.  
  190.      if (GETBIT(firstDetail.pMask, secondDetail.exact))
  191.         return TRUE;
  192.     }
  193.     
  194.     return FALSE;
  195. }
  196.  
  197. static Bool 
  198. IdenticalExactDetails(firstExact, secondExact, exception)
  199.     unsigned short firstExact, secondExact, exception;
  200. {
  201.     if ((firstExact == exception) || (secondExact == exception))
  202.     return FALSE;
  203.    
  204.     if (firstExact == secondExact)
  205.     return TRUE;
  206.  
  207.     return FALSE;
  208. }
  209.  
  210. static Bool 
  211. DetailSupersedesSecond(firstDetail, secondDetail, exception)
  212.     DetailRec firstDetail, secondDetail;
  213.     unsigned short exception;
  214. {
  215.     if (IsInGrabMask(firstDetail, secondDetail, exception))
  216.     return TRUE;
  217.  
  218.     if (IdenticalExactDetails(firstDetail.exact, secondDetail.exact,
  219.                   exception))
  220.     return TRUE;
  221.   
  222.     return FALSE;
  223. }
  224.  
  225. static Bool
  226. GrabSupersedesSecond(pFirstGrab, pSecondGrab)
  227.     GrabPtr pFirstGrab, pSecondGrab;
  228. {
  229.     if (!DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  230.                 pSecondGrab->modifiersDetail, 
  231.                 (unsigned short)AnyModifier))
  232.     return FALSE;
  233.  
  234.     if (DetailSupersedesSecond(pFirstGrab->detail,
  235.                    pSecondGrab->detail, (unsigned short)AnyKey))
  236.     return TRUE;
  237.  
  238.     return FALSE;
  239. }
  240.  
  241. Bool
  242. GrabMatchesSecond(pFirstGrab, pSecondGrab)
  243.     GrabPtr pFirstGrab, pSecondGrab;
  244. {
  245.     if ((pFirstGrab->device != pSecondGrab->device) ||
  246.     (pFirstGrab->modifierDevice != pSecondGrab->modifierDevice) ||
  247.     (pFirstGrab->type != pSecondGrab->type))
  248.     return FALSE;
  249.  
  250.     if (GrabSupersedesSecond(pFirstGrab, pSecondGrab) ||
  251.     GrabSupersedesSecond(pSecondGrab, pFirstGrab))
  252.     return TRUE;
  253.  
  254.     if (DetailSupersedesSecond(pSecondGrab->detail, pFirstGrab->detail,
  255.                    (unsigned short)AnyKey) 
  256.     && 
  257.     DetailSupersedesSecond(pFirstGrab->modifiersDetail,
  258.                    pSecondGrab->modifiersDetail,
  259.                    (unsigned short)AnyModifier))
  260.     return TRUE;
  261.  
  262.     if (DetailSupersedesSecond(pFirstGrab->detail, pSecondGrab->detail,
  263.                    (unsigned short)AnyKey)
  264.     && 
  265.     DetailSupersedesSecond(pSecondGrab->modifiersDetail,
  266.                    pFirstGrab->modifiersDetail,
  267.                    (unsigned short)AnyModifier))
  268.     return TRUE;
  269.  
  270.     return FALSE;
  271. }
  272.  
  273. int
  274. AddPassiveGrabToList(pGrab)
  275.     GrabPtr pGrab;
  276. {
  277.     GrabPtr grab;
  278.  
  279.     for (grab = wPassiveGrabs(pGrab->window); grab; grab = grab->next)
  280.     {
  281.     if (GrabMatchesSecond(pGrab, grab))
  282.     {
  283.         if (CLIENT_BITS(pGrab->resource) != CLIENT_BITS(grab->resource))
  284.         {
  285.         FreeGrab(pGrab);
  286.         return BadAccess;
  287.         }
  288.     }
  289.     }
  290.  
  291.     if (!pGrab->window->optional && !MakeWindowOptional (pGrab->window))
  292.     {
  293.     FreeGrab(pGrab);
  294.     return BadAlloc;
  295.     }
  296.     pGrab->next = pGrab->window->optional->passiveGrabs;
  297.     pGrab->window->optional->passiveGrabs = pGrab;
  298.     if (AddResource(pGrab->resource, RT_PASSIVEGRAB, (pointer)pGrab))
  299.     return Success;
  300.     return BadAlloc;
  301. }
  302.  
  303. /* the following is kinda complicated, because we need to be able to back out
  304.  * if any allocation fails
  305.  */
  306.  
  307. Bool
  308. DeletePassiveGrabFromList(pMinuendGrab)
  309.     GrabPtr pMinuendGrab;
  310. {
  311.     register GrabPtr grab;
  312.     GrabPtr *deletes, *adds;
  313.     Mask ***updates, **details;
  314.     int i, ndels, nadds, nups;
  315.     Bool ok;
  316.  
  317. #define UPDATE(mask,exact) \
  318.     if (!(details[nups] = DeleteDetailFromMask(mask, exact))) \
  319.       ok = FALSE; \
  320.     else \
  321.       updates[nups++] = &(mask)
  322.  
  323.     i = 0;
  324.     for (grab = wPassiveGrabs(pMinuendGrab->window); grab; grab = grab->next)
  325.     i++;
  326.     if (!i)
  327.     return TRUE;
  328.     deletes = (GrabPtr *)ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  329.     adds = (GrabPtr *)ALLOCATE_LOCAL(i * sizeof(GrabPtr));
  330.     updates = (Mask ***)ALLOCATE_LOCAL(i * sizeof(Mask **));
  331.     details = (Mask **)ALLOCATE_LOCAL(i * sizeof(Mask *));
  332.     if (!deletes || !adds || !updates || !details)
  333.     {
  334.     if (details) DEALLOCATE_LOCAL(details);
  335.     if (updates) DEALLOCATE_LOCAL(updates);
  336.     if (adds) DEALLOCATE_LOCAL(adds);
  337.     if (deletes) DEALLOCATE_LOCAL(deletes);
  338.     return FALSE;
  339.     }
  340.     ndels = nadds = nups = 0;
  341.     ok = TRUE;
  342.     for (grab = wPassiveGrabs(pMinuendGrab->window);
  343.      grab && ok;
  344.      grab = grab->next)
  345.     {
  346.     if ((CLIENT_BITS(grab->resource) != CLIENT_BITS(pMinuendGrab->resource)) ||
  347.         !GrabMatchesSecond(grab, pMinuendGrab))
  348.         continue;
  349.     if (GrabSupersedesSecond(pMinuendGrab, grab))
  350.     {
  351.         deletes[ndels++] = grab;
  352.     }
  353.     else if ((grab->detail.exact == AnyKey)
  354.          && (grab->modifiersDetail.exact != AnyModifier))
  355.     {
  356.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  357.     }
  358.     else if ((grab->modifiersDetail.exact == AnyModifier) 
  359.          && (grab->detail.exact != AnyKey))
  360.     {
  361.         UPDATE(grab->modifiersDetail.pMask,
  362.            pMinuendGrab->modifiersDetail.exact);
  363.     }
  364.     else if ((pMinuendGrab->detail.exact != AnyKey)
  365.          && (pMinuendGrab->modifiersDetail.exact != AnyModifier))
  366.     {
  367.         GrabPtr pNewGrab;
  368.  
  369.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  370.  
  371.         pNewGrab = CreateGrab(CLIENT_ID(grab->resource), grab->device,
  372.                   grab->window, (Mask)grab->eventMask,
  373.                   (Bool)grab->ownerEvents,
  374.                   (Bool)grab->keyboardMode,
  375.                   (Bool)grab->pointerMode,
  376.                   grab->modifierDevice,
  377.                   AnyModifier, (int)grab->type,
  378.                   pMinuendGrab->detail.exact,
  379.                   grab->confineTo, grab->cursor);
  380.         if (!pNewGrab)
  381.         ok = FALSE;
  382.         else if (!(pNewGrab->modifiersDetail.pMask =
  383.                DeleteDetailFromMask(grab->modifiersDetail.pMask,
  384.                      pMinuendGrab->modifiersDetail.exact))
  385.              ||
  386.              (!pNewGrab->window->optional &&
  387.               !MakeWindowOptional(pNewGrab->window)))
  388.         {
  389.         FreeGrab(pNewGrab);
  390.         ok = FALSE;
  391.         }
  392.         else if (!AddResource(pNewGrab->resource, RT_PASSIVEGRAB,
  393.                   (pointer)pNewGrab))
  394.         ok = FALSE;
  395.         else
  396.         adds[nadds++] = pNewGrab;
  397.     }   
  398.     else if (pMinuendGrab->detail.exact == AnyKey)
  399.     {
  400.         UPDATE(grab->modifiersDetail.pMask,
  401.            pMinuendGrab->modifiersDetail.exact);
  402.     }
  403.     else
  404.     {
  405.         UPDATE(grab->detail.pMask, pMinuendGrab->detail.exact);
  406.     }
  407.     }
  408.  
  409.     if (!ok)
  410.     {
  411.     for (i = 0; i < nadds; i++)
  412.         FreeResource(adds[i]->resource, RT_NONE);
  413.     for (i = 0; i < nups; i++)
  414.         xfree(details[i]);
  415.     }
  416.     else
  417.     {
  418.     for (i = 0; i < ndels; i++)
  419.         FreeResource(deletes[i]->resource, RT_NONE);
  420.     for (i = 0; i < nadds; i++)
  421.     {
  422.         grab = adds[i];
  423.         grab->next = grab->window->optional->passiveGrabs;
  424.         grab->window->optional->passiveGrabs = grab;
  425.     }
  426.     for (i = 0; i < nups; i++)
  427.     {
  428.         xfree(*updates[i]);
  429.         *updates[i] = details[i];
  430.     }
  431.     }
  432.     DEALLOCATE_LOCAL(details);
  433.     DEALLOCATE_LOCAL(updates);
  434.     DEALLOCATE_LOCAL(adds);
  435.     DEALLOCATE_LOCAL(deletes);
  436.     return ok;
  437.  
  438. #undef UPDATE
  439. }
  440. @
  441.